package com.huaxin.hxcloud.main;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaxin.common.AppManager;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Entity.BaseDTO;
import com.huaxin.common.Entity.HxUserTagBean;
import com.huaxin.common.tools.RequestHelper;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2017/9/28
 * @Desc 选择用户标签
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxUserTagActivity extends BaseActivity {

    @BindView(R.id.edit_img)
    ImageView     mEditImg;
    @BindView(R.id.edit_frame)
    FrameLayout   mEditFrame;
    @BindView(R.id.title_tv)
    TextView      mTitleTv;
    @BindView(R.id.finish_btn)
    Button        mFinishBtn;
    @BindView(R.id.flow_tag_layout)
    TagFlowLayout mFlowTagLayout;
    private List<HxUserTagBean.DataBean>       mDataSource;
    private TagAdapter<HxUserTagBean.DataBean> mTagAdapter;
    private static final String TAG = "HxUserTagActivity";
    private int mUserId;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_user_tag;
    }


    @Override
    public void initData() {
        Intent intent = getIntent();
        mUserId = intent.getIntExtra("USER_ID", 0);
        mDataSource = new ArrayList<>();
        //item_user_tag
        //只能选择5个
        mFlowTagLayout.setMaxSelectCount(5);
        mTagAdapter = new TagAdapter<HxUserTagBean.DataBean>(mDataSource) {
            @Override
            public View getView(FlowLayout parent, int position, HxUserTagBean.DataBean dataBean) {
                TextView tv = (TextView) mInflater.inflate(R.layout.item_user_tag,
                        mFlowTagLayout, false);
                tv.setText(dataBean.getName());
                return tv;
            }

        };

        mFlowTagLayout.setAdapter(mTagAdapter);
    }

    @Override
    protected void requestData() {
        super.requestData();
        Call<HxUserTagBean> specialFind = RequestHelper.getRequestApi().getSpecialFind();
        specialFind.enqueue(new Callback<HxUserTagBean>() {
            @Override
            public void onResponse(Call<HxUserTagBean> call, Response<HxUserTagBean> response) {
                if (response.isSuccessful()) {
                    List<HxUserTagBean.DataBean> dataBeens = response.body().getData();
                    if (dataBeens.size() > 0) {
                        mDataSource.addAll(dataBeens);
                        mTagAdapter.notifyDataChanged();
                    }

                } else {
                    ToastUtil.showToast("网络故障");
                }
            }

            @Override
            public void onFailure(Call<HxUserTagBean> call, Throwable t) {
                ToastUtil.showToast("网络故障");
            }
        });
    }


    @OnClick({R.id.edit_frame, R.id.finish_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_frame: //取消
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.finish_btn://完成
                if (mFlowTagLayout.getSelectedList().size() > 1) {
                    submitFlag();
                } else {
                    AppManager.getAppManager().finishActivity();
                }
                break;
        }
    }

    /**
     * 提交标签给服务器
     */
    public void submitFlag() {
        Set<Integer> selectedList = mFlowTagLayout.getSelectedList();
        Iterator<Integer> iterator = selectedList.iterator();
        List<Integer> flagData = new ArrayList<>();
        while (iterator.hasNext()) {//遍历
            flagData.add(mDataSource.get(iterator.next()).getId());
        }
        String falgStr = flagData.toString().substring(1, flagData.toString().length() - 1);

        Call<BaseDTO> baseDTOCall = RequestHelper.getRequestApi().putCommitSpecial(mUserId, falgStr);
        baseDTOCall.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
                if (response.isSuccessful()) {
                    ToastUtil.showToast("提交成功");
                    AppManager.getAppManager().finishActivity();
                } else {
                    ToastUtil.showToast("网络故障");
                }
            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                ToastUtil.showToast("网络故障");
            }
        });
    }


}
